package com.franklin.ideaplugin.mybatisrunner.psi;

import cn.hutool.crypto.SecureUtil;
import com.franklin.ideaplugin.api.psi.IMyPsiElement;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiElement;
import com.intellij.psi.xml.XmlTag;
import lombok.Getter;
import lombok.Setter;

/**
 * @author Ye Junhui
 * @since 2024/2/20
 */
@Getter
public abstract class MybatisPsiElement<T extends PsiElement> implements IMyPsiElement<T> {

    /**
     * 元素
     */
    protected final T psiElement;

    protected final Project project;

    protected String key;

    @Setter
    protected XmlTag sqlXmlTag;

    protected MybatisPsiElement(T psiElement) {
        this.psiElement = psiElement;
        this.project = psiElement.getProject();
    }

    @Override
    public T getPsiElement() {
        return psiElement;
    }

    @Override
    public Project getProject() {
        return project;
    }

    /**
     * 构建SQL脚本
     * @return
     */
    public abstract String buildSqlScript();

    /**
     * 构建Key
     * @return
     */
    public String buildKey(){
        return SecureUtil.md5(key);
    }

    public static MybatisPsiElement buildMybatisElement(PsiElement psiElement){
        if (psiElement instanceof PsiAnnotation){
            return new MybatisPsiAnnotation((PsiAnnotation) psiElement);
        }
        return new MybatisXmlTag((XmlTag) psiElement);
    }
}
